# @Time    :2025/4/3 10:21
import os
from collections.abc import Iterable, Iterator

for element in [1, 2, 3]:
    print(element)
for element in (1, 2, 3):
    print(element)
for key in {"one": 1, "two": 2}:
    print(key)
for char in "123":
    print(char)

with open("myfile.txt", "w") as f:
    f.write("H\ne\nl\nl\no\n \nW\no\nr\nl\nd\n")
for line in open("myfile.txt"):
    print(line, end="")
os.remove("myfile.txt")

#判断是否是 迭代器（名词） Iterator  ，容器本身不是迭代器

print(isinstance([], Iterator))  # False
print(isinstance((), Iterator))  # False
print(isinstance(set(), Iterator))  # False
print(isinstance({}, Iterator))  # False
print(isinstance("abc", Iterator))  # False

print(isinstance(100, Iterable))  # False
print("---"*40)
# #判断是否是可迭代的对象  Iterable

print(isinstance([], Iterable))  # True
print(isinstance((), Iterable))  # True
print(isinstance(set(), Iterable))  # True
print(isinstance({}, Iterable))  # True




print(isinstance(iter([]), Iterator))  # True

print(isinstance(100, Iterable))  # False

print("-"*10,"---next-------")
#next 是对元素的获取
"""
自己创建迭代器对象
"""
list = [1, 2, 3]
it = iter(list)  # 创建迭代器对象
print(type(it))
print(next(it))  # 输出迭代器的下一个元素,1
# print(next(it))  # 2
# print(next(it))  # 3
# print(next(it))  # StopIteration


#这个for的底层就是上边那样的 底层调iter()、next()
for i in list:
    print(i)


